﻿@model OrderModel.AddOrderProductModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderNumber);
}

<div class="row">
    <div class="col-md-12">
        <div class="x_panel light form-fit">
            <div class="x_title">
                <div class="caption">
                    <i class="fa fa-cube"></i>
                    @string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderNumber)
                    <small>
                        <i class="fa fa-arrow-circle-left"></i>
                        @Html.ActionLink(T("Admin.Orders.Products.AddNew.BackToOrder").Text, "Edit", new { id = Model.OrderId })
                    </small>
                </div>
            </div>
            <div class="x_content form">
                <vc:admin-widget widget-zone="order_add_products_top" additional-data="Model" />
                <div class="form-horizontal">
                    <div class="form-body">
                        <div class="form-group popup">
                            <admin-label asp-for="SearchProductName" />
                            <div class="col-xs-8 col-md-4 col-sm-4">
                                <admin-input asp-for="SearchProductName" />
                            </div>
                            <div class="col-xs-8 col-md-4 col-sm-4 mt-mobile-10">
                                <div class="btn-group btn-group-devided">
                                    <button class="btn btn-success filter-submit" id="search-products">
                                        <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                    </button>
                                    <button type="button" class="btn btn-default filter-extend-button">
                                        <i class="fa fa-filter"></i>
                                    </button>
                                    <button class="btn btn-success" type="submit" name="save"><i class="fa fa-check"></i> @T("Admin.Common.Save") </button>
                                </div>
                            </div>
                        </div>
                        <div class="form-group extend-collapse">
                            <admin-label asp-for="SearchCategoryId" />
                            <div class="col-md-9 col-sm-9">
                                <admin-select asp-for="SearchCategoryId" asp-items="Model.AvailableCategories" />
                            </div>
                        </div>
                        <div class="form-group extend-collapse">
                            <admin-label asp-for="SearchManufacturerId" />
                            <div class="col-md-9 col-sm-9">
                                <admin-select asp-for="SearchManufacturerId" asp-items="Model.AvailableManufacturers" />
                            </div>
                        </div>
                        <div class="form-group extend-collapse">
                            <admin-label asp-for="SearchProductTypeId" />
                            <div class="col-md-9 col-sm-9">
                                <admin-select asp-for="SearchProductTypeId" asp-items="Model.AvailableProductTypes" />
                            </div>
                        </div>
                    </div>
                </div>
                <vc:admin-widget widget-zone="order_add_products_bottom" additional-data="Model" />
            </div>
            <div class="x_content">
                <strong>@T("Admin.Orders.Products.AddNew.Note1")</strong>
                <div id="products-grid"></div>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {

        $('.extend-collapse').hide();
        $('.filter-extend-button').click(function () {
            $('.extend-collapse').toggle("slow");
            $('.filter-text-close').toggle();
        });

        $("#products-grid").kendoGrid({
            dataSource: {
                transport: {
                    read: {
                        url: "@Html.Raw(Url.Action("AddProductToOrder", "Order"))",
                        type: "POST",
                        dataType: "json",
                        data: additionalData
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors"
                },
                error: function(e) {
                    display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                },
                pageSize: @(adminAreaSettings.DefaultGridPageSize),
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            pageable: {
                refresh: true,
                pageSizes: [@(adminAreaSettings.GridPageSizes)]
            },
            editable: {
                confirmation: false,
                mode: "inline"
            },
            scrollable: false,
            //selectable
            selectable: true,
            change: onChange,
            columns: [{
                field: "Name",
                title: "@T("Admin.Orders.Products.AddNew.Name")",
                width: 400
            },{
                field: "Sku",
                title: "@T("Admin.Orders.Products.AddNew.SKU")",
                width: 100
            }]
        });
    });
</script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#search-products').click(function () {
            var grid = $('#products-grid').data('kendoGrid');
            grid.dataSource.page(1); //new search. Set page size to 1
            //grid.dataSource.read(); we already loaded the grid above using "page" function
            return false;
        });
    });

    $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
        if (event.keyCode == 13) {
            $("#search-products").click();
            return false;
        }
    });

    function additionalData() {
        var data = {
            SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
            SearchCategoryId: $('#SearchCategoryId').val(),
            SearchManufacturerId: $('#SearchManufacturerId').val(),
            SearchProductTypeId: $('#SearchProductTypeId').val()
        };
        addAntiForgeryToken(data);
        return data;
    }

    function onChange(e) {
        var grid = $('#products-grid').data('kendoGrid');
        var selectedItem = grid.dataItem(grid.select());
        var productId = selectedItem.Id;
        //load "product details page" block
        var productDetailsActionUrl = '@Html.Raw(Url.Action("AddProductToOrderDetails", "Order", new { orderId = Model.OrderId, productId = "productidplaceholder" }))';
        productDetailsActionUrl = productDetailsActionUrl.replace("productidplaceholder", productId);
        setLocation(productDetailsActionUrl);
    }
</script>
<div id="productdetails">
</div>
<input asp-for="OrderId" type="hidden" />
